package com.mytransaction.myaop;

import com.mytransaction.Util.myUtils;
import com.mytransaction.jdbcpackage.imple.MyTransation;
import com.mytransaction.myAnnotation.MyAnt;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.lang.reflect.Method;

@Component
@Aspect
public class MyAop {
    @Autowired
    private MyTransation myTransation;
    @Autowired
    private TransactionStatus transactionStatus;
    @Before("execution(* com.mytransaction.mytests.*(..))")
    public void before(){ }

    @AfterReturning("execution(* com.mytransaction.mytests.*(..))")
    public void returning(){ }

    @After("execution(* com.mytransaction.mytests.*(..))")
    public void after(){ }

    @AfterThrowing("execution(* com.mytransaction.mytests.*(..))")
    public void Throwing(){ myTransation.rollBack(transactionStatus);}


    @Around("execution(* com.mytransaction.mytests.*(..))")
    public void aVoid(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //获取方法上的注解
        MyAnt myAnt= myUtils.getextTransaction(proceedingJoinPoint);
        //开启事务
        if (myAnt!=null){
            transactionStatus=myTransation.begin();
        }
        proceedingJoinPoint.proceed();
        if (transactionStatus!=null){
            myTransation.commit(transactionStatus);
        }
    }



}
